﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace StatusPlan3
{
    /// <summary>
    /// Interaction logic for Window_ChangePassword.xaml
    /// </summary>
    public partial class Window_ChangePassword : Window
    {
        User user;

        public Window_ChangePassword()
        {
            InitializeComponent();
        }

        public void showChangePassword(User u)
        {
            user = u;
            TextBlock_Title.Text = u.fullName + " skal broyta kotu";
            FirstPasswordTextBox.Focus();
        }

        private void CancelButto_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        private void OkButto_Click(object sender, RoutedEventArgs e)
        {
            if (FirstPasswordTextBox.Password.Trim().Length == 0)
            {
                MessageBox.Show("Skal fylla kotu feltið út", "Feilur", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else if (SecondPasswordTextBox.Password.Trim().Length == 0)
            {
                MessageBox.Show("Skal fylla annað kotu feltið út", "Feilur", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else if (FirstPasswordTextBox.Password != SecondPasswordTextBox.Password)
            {
                MessageBox.Show("Kota 1 og kota 2 eru ikki líka", "Feilur", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else
            {
                user.password = FirstPasswordTextBox.Password;
                user.lastChangedPassword = DateTime.Now;
                user.changePassword = false;

                StatusPlanData.statusPlanHandler.saveUser(user);
                
                this.DialogResult = true;
                this.Close();
            }
        }

        private void Window_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            FirstPasswordTextBox.Focus();
        }

        private void FirstPasswordTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.Key == Key.Enter)
                SecondPasswordTextBox.Focus();
        }

        private void TextBlock_KeyDown(object sender, KeyEventArgs e)
        {
            
        }

        private void SecondPasswordTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                OkButto_Click(null, null);
        }
    }
}
